package com.dmxtechnologies.community.monitor.ejb.impl;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

import com.dmxtechnologies.community.monitor.ejb.CMXMonitorBroadcastRemote;
import com.dmxtechnologies.community.monitor.ejb.bean.MonitorRestClient;
import com.dmxtechnologies.community.monitor.ejb.pojo.MonitorEntity;

/**
 * Session Bean implementation class CMXMonitorBroadcast
 */
@Singleton
public class CMXMonitorBroadcast implements CMXMonitorBroadcastRemote {
	
	private static final Logger log = Logger.getLogger("CMXMonitorBroadcast");
	
	private Map<String, List<MonitorEntity>> maps;
	
    /**
     * Default constructor. 
     */
    public CMXMonitorBroadcast() {
    }
    
    @Schedule(minute = "*/5", hour = "*", persistent = false)
    public void timer(){
    	maps = new HashMap<String, List<MonitorEntity>>();
    	
    	String url = "http://localhost:4848/monitoring/domain/server/jvm/memory/usedheapsize-count";
    	maps.put("usedheapsize-count", getMonitorEntityList(url));
    	
    	String url1 = "http://localhost:4848/monitoring/domain/server/jvm/memory/maxheapsize-count";
    	maps.put("maxheapsize-count", getMonitorEntityList(url1));
    	
		String url2 = "http://localhost:4848/monitoring/domain/server/network/admin-listener/connection-queue/countqueued";
		maps.put("countqueued", getMonitorEntityList(url2));
		
		String url3 = "http://localhost:4848/monitoring/domain/server/network/admin-listener/connection-queue/maxqueued";
		maps.put("maxqueued", getMonitorEntityList(url3)); 
		
    }
    
	private List<MonitorEntity> getMonitorEntityList(String url) {
		String jsonStr = MonitorRestClient.sendMonitorRequestToEIS(url);
		List<MonitorEntity> monitorEntities = new ArrayList<>();
		if(jsonStr != null && !"".equals(jsonStr)){
			JsonReader jsonReader = Json.createReader(new StringReader(jsonStr));
			JsonObject jsonObject = jsonReader.readObject();
			JsonObject jsonObject2 = jsonObject.getJsonObject("extraProperties");
			JsonObject jsonObject3 = jsonObject2.getJsonObject("entity");
			for(String key : jsonObject3.keySet()){
				JsonObject jsonObject4 = jsonObject3.getJsonObject(key);
				MonitorEntity entity = new MonitorEntity();
				entity.setCount(jsonObject4.keySet().contains("count")?jsonObject4.getInt("count"):0);
				entity.setDescription(jsonObject4.keySet().contains("description")?jsonObject4.getString("description"):"");
				entity.setLastsampletime(jsonObject4.keySet().contains("lastsampletime")?jsonObject4.getJsonNumber("lastsampletime").longValue():0);
				entity.setName(jsonObject4.keySet().contains("name")?jsonObject4.getString("name"):"");
				entity.setStarttime(jsonObject4.keySet().contains("starttime")?jsonObject4.getJsonNumber("starttime").longValue():0);
				entity.setUnit(jsonObject4.keySet().contains("unit")?jsonObject4.getString("unit"):"");
				entity.setCount(jsonObject4.keySet().contains("highwatermark")?jsonObject4.getInt("highwatermark"):0);
				try {
					entity.setCurrent(jsonObject4.keySet().contains("current")?jsonObject4.getString("current"):"");
				} catch (Exception e) {
					entity.setCurrent(jsonObject4.keySet().contains("current")?String.valueOf(jsonObject4.getInt("current")):"");
				}
				entity.setLowwatermark(jsonObject4.keySet().contains("lowwatermark")?jsonObject4.getInt("lowwatermark"):0);
				monitorEntities.add(entity);
			}
		}
		
		log.info("invoke success" + monitorEntities.size());
		return monitorEntities;
	}
 
	@Override
	public Map<String, List<MonitorEntity>> getMonitorInfo() {
		if(maps == null){
			this.timer();
		}
		return maps;
	}
}
